home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / comm / misc / CapiRexxVoiceM.lha / alaw.c next >
Encoding:
C/C++ Source or Header  |  1995-10-07  |  5.8 KB  |  282 lines

  1. /*
  2. **
  3. **    $Id: alaw.c,v 1.2 1995/10/07 02:52:10 chris Exp $
  4. **    $Revision: 1.2 $
  5. **
  6. **    $Filename: developer/alaw.c $
  7. **    $Author: chris $
  8. **    $Date: 1995/10/07 02:52:10 $
  9. **
  10. **    Convert A-Law to 16-bit and 8-bit linear audio format
  11. **
  12. **    COPYRIGHT (C) 1992-1994 BY CHRISTIAN A. WEBER, ZUERICH, SWITZERLAND. ALL
  13. **    RIGHTS RESERVED. NO PART OF THIS SOFTWARE MAY BE COPIED, REPRODUCED,  OR
  14. **    TRANSMITTED IN ANY FORM OR BY ANY MEANS,  WITHOUT THE PRIOR WRITTEN PER-
  15. **    MISSION OF THE AUTHOR. NO WARRANTY. USE AT YOUR OWN RISK.
  16. **
  17. */
  18.  
  19. #include "os.h"
  20.  
  21. #define  _ALAW
  22. #include "alaw.h"
  23.  
  24.  
  25. static S16    *table_alaw_to_16bit;
  26. static U8    *table_16bit_to_alaw;
  27.  
  28.  
  29. static S16 ALaw2Sample( S16 code )
  30. {
  31.     S16 sample;
  32.  
  33.     /* Revert bit order */
  34.     code = (code<<4 & 0xf0) | (code>>4 & 0x0f);
  35.     code = (code<<2 & 0xcc) | (code>>2 & 0x33);
  36.     code = (code<<1 & 0xaa) | (code>>1 & 0x55);
  37.  
  38.     /* Invert even bits: */
  39.     code ^= 0x55;
  40.  
  41.     /* Positive <-> Negative: */
  42.     if (code & 0x80)            /* positive A-Law range */
  43.         code &= ~0x80;
  44.     else                        /* negative A-Law range */
  45.         code = ~code;            /* Dasselbe wie -code-1; */
  46.  
  47.     /* Gradient equations (Steigungsgleichungen) */
  48.     if (code >= 112)
  49.         sample = 16384  + (code-112) * 1024;
  50.     else if (code >= 96)
  51.         sample = 8192   + (code-96)  * 512;
  52.     else if (code >= 80)
  53.         sample = 4096   + (code-80)  * 256;
  54.     else if (code >= 64)
  55.         sample = 2048   + (code-64)  * 128;
  56.     else if (code >= 48)
  57.         sample = 1024   + (code-48)  * 64;
  58.     else if (code >= 32)
  59.         sample = 512    + (code-32)  * 32;
  60.     else if (code >= -32)
  61.         sample = -512   + (code+32)  * 16;
  62.     else if (code >= -48)
  63.         sample = -1024  + (code+48)  * 32;
  64.     else if (code >= -64)
  65.         sample = -2048  + (code+64)  * 64;
  66.     else if (code >= -80)
  67.         sample = -4096  + (code+80)  * 128;
  68.     else if (code >= -96)
  69.         sample = -8192  + (code+96)  * 256;
  70.     else if (code >= -112)
  71.         sample = -16384 + (code+112) * 512;
  72.     else
  73.         sample = -32768 + (code+128) * 1024;
  74.  
  75.     return sample;
  76. }
  77.  
  78.  
  79. static U8 Sample2ALaw( S16 sample )
  80. {
  81.     S16 code;
  82.  
  83.     /* Gradient equations (Steigungsgleichungen) */
  84.     if (sample >= 16384)
  85.         code =  112 + (sample-16384) / 1024;
  86.     else if (sample >= 8192)
  87.         code =   96 + (sample-8192)  / 512;
  88.     else if (sample >= 4096)
  89.         code =   80 + (sample-4096)  / 256;
  90.     else if (sample >= 2048)
  91.         code =   64 + (sample-2048)  / 128;
  92.     else if (sample >= 1024)
  93.         code =   48 + (sample-1024)  / 64;
  94.     else if (sample >= 512)
  95.         code =   32 + (sample-512)   / 32;
  96.     else if (sample >= -512)
  97.         code =  -32 + (sample+512)   / 16;
  98.     else if (sample >= -1024)
  99.         code =  -48 + (sample+1024)  / 32;
  100.     else if (sample >= -2048)
  101.         code =  -64 + (sample+2048)  / 64;
  102.     else if (sample >= -4096)
  103.         code =  -80 + (sample+4096)  / 128;
  104.     else if (sample >= -8192)
  105.         code =  -96 + (sample+8192)  / 256;
  106.     else if (sample >= -16384)
  107.         code = -112 + (sample+16384) / 512;
  108.     else
  109.         code = -128 + (sample+32768) / 1024;
  110.  
  111.     /* Positive <-> Negative: */
  112.     if(code < 0)            /* negative A-Law range */
  113.         code = ~code;
  114.     else                    /* positive A-Law range */
  115.         code |= 0x80;
  116.  
  117.     /* Invert even bits: */
  118.     code ^= 0x55;
  119.  
  120.     /* Revert bit order */
  121.     code = (code<<4 & 0xf0) | (code>>4 & 0x0f);
  122.     code = (code<<2 & 0xcc) | (code>>2 & 0x33);
  123.     code = (code<<1 & 0xaa) | (code>>1 & 0x55);
  124.  
  125.     return (U8)code;
  126. }
  127.  
  128.  
  129. /****** ALAW_Init ***********************************************************
  130. *
  131. *   NAME
  132. *       ALAW_Init -- Initialize the ALAW module
  133. *
  134. *   SYNOPSIS
  135. *       ALAW_Init()
  136. *
  137. *       VOID ALAW_Init( VOID );
  138. *
  139. *   FUNCTION
  140. *       Call this once before any other ALAW function.
  141. *
  142. *   INPUTS
  143. *
  144. *   RESULTS
  145. *
  146. *   SEE ALSO
  147. *       ALAW_Exit()
  148. *
  149. ****************************************************************************/
  150.  
  151. VOID
  152. ALAW_Init( VOID )
  153. {
  154.     ALAW_Exit();
  155.  
  156.     if (table_alaw_to_16bit = OS_Malloc( 256 * sizeof( S16 ) ))
  157.     {
  158.         S16 *ptr = table_alaw_to_16bit, i;
  159.  
  160.         for (i = 0; i < 256; ++i, ++ptr)
  161.             *ptr = ALaw2Sample( i );
  162.     }
  163.  
  164.     if (table_16bit_to_alaw = OS_Malloc( 65536 * sizeof( U8 ) ))
  165.     {
  166.         U8    *ptr = table_16bit_to_alaw;
  167.         S32 i;
  168.  
  169.         for (i = 0; i < 65536L; ++i, ++ptr)
  170.             *ptr = Sample2ALaw( (S16)i );
  171.     }
  172. }
  173.  
  174.  
  175. /****** ALAW_Exit ***********************************************************
  176. *
  177. *   NAME
  178. *       ALAW_Exit -- Clean up the ALAW module
  179. *
  180. *   SYNOPSIS
  181. *       ALAW_Exit()
  182. *
  183. *       VOID ALAW_Exit( VOID );
  184. *
  185. *   FUNCTION
  186. *       Call this once at the end.
  187. *
  188. *   INPUTS
  189. *
  190. *   RESULTS
  191. *
  192. *   SEE ALSO
  193. *       ALAW_Exit()
  194. *
  195. ****************************************************************************/
  196.  
  197. VOID
  198. ALAW_Exit( VOID )
  199. {
  200.     if (table_alaw_to_16bit)
  201.     {
  202.         OS_Free( table_alaw_to_16bit );
  203.         table_alaw_to_16bit = NULL;
  204.     }
  205.  
  206.     if (table_16bit_to_alaw)
  207.     {
  208.         OS_Free( table_16bit_to_alaw );
  209.         table_16bit_to_alaw = NULL;
  210.     }
  211. }
  212.  
  213.  
  214. /****** ALAW_ALawTo16Bit ****************************************************
  215. *
  216. *   NAME
  217. *       ALAW_ALawTo16Bit -- Convert A-Law audio to 16 bit linear format
  218. *
  219. *   SYNOPSIS
  220. *       ALAW_ALawTo16Bit( from, to, size )
  221. *
  222. *       void ALAW_ALawTo16Bit( U8 *, S16 *, U32 );
  223. *
  224. *   FUNCTION
  225. *       Convert A-Law audio to 16 bit linear S16s.
  226. *
  227. *   INPUTS
  228. *
  229. *   RESULTS
  230. *
  231. *   SEE ALSO
  232. *       ALAW_16BitToALaw()
  233. *
  234. ****************************************************************************/
  235.  
  236. VOID
  237. ALAW_ALawTo16Bit( U8 *alaw, S16 *dest, U32 size )
  238. {
  239.     U8 *send = alaw + size;
  240.  
  241.     while (alaw < send)
  242.     {
  243.         *dest++ = table_alaw_to_16bit[*alaw++];
  244.     }
  245. }
  246.  
  247.  
  248. /****** ALAW_16BitToALaw ****************************************************
  249. *
  250. *   NAME
  251. *       ALAW_16BitToALaw -- Convert 16 bit linear format to A-Law audio
  252. *
  253. *   SYNOPSIS
  254. *       ALAW_16BitToALaw( from, to, size )
  255. *
  256. *       void ALAW_16BitToALaw( S16 *, U8 *, U32 );
  257. *
  258. *   FUNCTION
  259. *       Convert 16 bit linear signed to A-Law
  260. *
  261. *   INPUTS
  262. *
  263. *   RESULTS
  264. *
  265. *   SEE ALSO
  266. *       ALAW_ALawTo16Bit()
  267. *
  268. ****************************************************************************/
  269.  
  270. VOID
  271. ALAW_16BitToALaw( S16 *from, U8 *alaw, U32 size )
  272. {
  273.     U8 *dend = alaw + size;
  274.  
  275.     while (alaw < dend)
  276.     {
  277.         *alaw++ = table_16bit_to_alaw[(U16)*from++];
  278.     }
  279. }
  280.  
  281.  
  282.